package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.domain.WyGreenManagement;
import com.ruoyi.park.domain.WyRequisitionDetail;
import com.ruoyi.park.mapper.WyGreenManagementMapper;
import com.ruoyi.park.mapper.WyRequisitionDetailMapper;
import com.ruoyi.park.service.IWyGreenManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 绿化管理Service业务层处理
 *
 * @author cjie
 * @date 2021-07-22
 */
@Service
public class WyGreenManagementServiceImpl implements IWyGreenManagementService {
    @Autowired
    private WyGreenManagementMapper wyGreenManagementMapper;
    @Autowired
    private WyRequisitionDetailMapper wyRequisitionDetailMapper;

    /**
     * 查询绿化管理
     *
     * @param id 绿化管理ID
     * @return 绿化管理
     */
    @Override
    public WyGreenManagement selectWyGreenManagementById(String id) {
        WyGreenManagement wyGreenManagement = wyGreenManagementMapper.selectWyGreenManagementById(id);
        WyRequisitionDetail queryDetail = new WyRequisitionDetail();
        queryDetail.setLinkId(id);
        wyGreenManagement.setRequisitionDetailList(wyRequisitionDetailMapper.selectWyRequisitionDetailList(queryDetail));

        return wyGreenManagement;
    }

    /**
     * 查询绿化管理列表
     *
     * @param wyGreenManagement 绿化管理
     * @return 绿化管理
     */
    @Override
    public List<WyGreenManagement> selectWyGreenManagementList(WyGreenManagement wyGreenManagement) {
        return wyGreenManagementMapper.selectWyGreenManagementList(wyGreenManagement);
    }

    /**
     * 新增绿化管理
     *
     * @param wyGreenManagement 绿化管理
     * @return 结果
     */
    @Override
    public int insertWyGreenManagement(WyGreenManagement wyGreenManagement) {
        wyGreenManagement.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyGreenManagement.setCreateTime(DateUtils.getNowDate());
        wyGreenManagement.setCreateBy(SecurityUtils.getUsername());

        //插入领料详情
        insertRequisitionDetail(wyGreenManagement);
        return wyGreenManagementMapper.insertWyGreenManagement(wyGreenManagement);
    }

    /**
     * 插入领料详情
     * @param wyGreenManagement
     */
    private void insertRequisitionDetail(WyGreenManagement wyGreenManagement) {
        List<WyRequisitionDetail> requisitionDetailList = wyGreenManagement.getRequisitionDetailList();
        if(requisitionDetailList != null && requisitionDetailList.size()>0){
            requisitionDetailList.forEach(wyRequisitionDetail ->{
                wyRequisitionDetail.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
                wyRequisitionDetail.setLinkId(wyGreenManagement.getId());

                wyRequisitionDetailMapper.insertWyRequisitionDetail(wyRequisitionDetail);
            });
        }
    }

    /**
     * 修改绿化管理
     *
     * @param wyGreenManagement 绿化管理
     * @return 结果
     */
    @Override
    public int updateWyGreenManagement(WyGreenManagement wyGreenManagement) {
        wyGreenManagement.setUpdateTime(DateUtils.getNowDate());
        wyGreenManagement.setUpdateBy(SecurityUtils.getUsername());

        List<WyRequisitionDetail> requisitionDetailList = wyGreenManagement.getRequisitionDetailList();
        if(requisitionDetailList != null && requisitionDetailList.size()>0){
            wyRequisitionDetailMapper.deleteDetailByLinkId(wyGreenManagement.getId());

            insertRequisitionDetail(wyGreenManagement);
        }
        return wyGreenManagementMapper.updateWyGreenManagement(wyGreenManagement);
    }

    /**
     * 批量删除绿化管理
     *
     * @param ids 需要删除的绿化管理ID
     * @return 结果
     */
    @Override
    public int deleteWyGreenManagementByIds(String[] ids) {
        return wyGreenManagementMapper.deleteWyGreenManagementByIds(ids);
    }

    /**
     * 删除绿化管理信息
     *
     * @param id 绿化管理ID
     * @return 结果
     */
    @Override
    public int deleteWyGreenManagementById(String id) {
        return wyGreenManagementMapper.deleteWyGreenManagementById(id);
    }
}
